const baseUrl = `${window.location.origin}/`;
layui.use(['jquery','upload','element','table','layer','form','util'], function(){
    var table = layui.table,
        layer = layui.layer;
    var upload = layui.upload;
    var form=layui.form;
    var util = layui.util;
    $ = layui.jquery;

    // loadLineList();

    table.render({
        elem: '#insLineRecordsTable'
        ,url: baseUrl+'/insLineRecord/listByCondition'
        ,method: 'post'
        ,title: '巡检线路记录表'
        ,cols: [[
            {type:'numbers',title: '序号'},
            {type:'checkbox'},
            {field:'TASKID', title:'记录id',hide:true},
            {field: 'POINTID', title: '线路id',hide:true},
            {field:'LINENAME', title:'线路名称',align : 'center' },
            {field:'POINTNUMBER', title:'点位数量',align : 'center' },
            {field:'INSPECTTIME', title:'巡检时长(min)',align : 'center' },
            {field:'INSPECTERRORTIME', title:'误差时长(min)',align : 'center' }
            ,{field:'INSPECTPERSON', title:'巡检人员', align : 'center' }
            ,{field:'FIRSTINSTIME', title:'第一个点位打卡时间', align : 'center' ,templet:function(d){
                    return formatTime(d.FIRSTINSTIME);
                } }
            ,{field:'LASTINSTIME', title:'最后一个点位打卡时间', align : 'center',templet:function(d){
                    return formatTime(d.LASTINSTIME);
                } }
            ,{field:'ACTINSTIME', title:'实际时长(min)', align : 'center' }
            ,{field:'INSPECTRESULT',align : 'center', title:'巡检结果',templet:function(d){
                    return parseResult(d.INSPECTRESULT);
                }}
            ,{field:'IFMISS',align : 'center', title:'是否漏巡',templet:function(d){
                    return parseIfMiss(d.IFMISS);
                }}
            ,{fixed: 'right', title:'操作', align : 'center',toolbar: '#barCmdOperate', width:220}
        ]]
        ,page: true
    });
    function formatTime(time){
        if(time){
            return util.toDateString(time, "yyyy-MM-dd HH:mm:ss");
        }
        return "";
    }
    // 搜索
    form.on('submit(search)',function(data){
        active['reload'].call(this);
        return false;
    });
    var active = {
        reload:function () {
            var lineName = $("#line_name").val();
            var inspect_result = $("#inspect_result").val();
            var inspect_person = $("#inspect_person").val();
            var if_miss = $("#if_miss").val();
            //执行重载
            table.reload('insLineRecordsTable', {
                where: {lineName:lineName,inspectResult:inspect_result,
                    inspectPerson:inspect_person, ifMiss:if_miss}
            }, 'data');
        }
    };

    //头工具栏事件
    table.on('tool(insLineRecordsTable)', function(obj){
        //记录查看
        switch(obj.event){
            case 'recordDetail':
                showRecordsDetail(obj.data.TASKID, obj.data.IFMISS);
                break;
            case 'recordDelete':
                deleteRecord(obj.data.record_id, obj.data.point_id, obj.data.mapping_id);
                break;
        }
    });

    function showRecordsDetail(recordId, ifMiss) {
        table.render({
            elem: '#xjxmTable',
            url: baseUrl+"/insLineRecord/listPointResults?recordId="+recordId,
            method: 'get',
            cols: [[
                {type:'numbers',title: '序号'},
                {field: 'point_id', title: '点位id',hide:true},
                {field: 'point_name', title: '点位名称',align:'center'},
                {field: 'state', title: '巡检状态',align:'center', templet:function(d){
                        return parseState(d.record_inspect_time);
                }},
                {field: 'record_inspect_time', title: '巡检时间',align:'center', templet:function(d){
                        return parseTime(d.record_inspect_time);
                }}
            ]],
        });
        layer.open({
            type: 1,
            title:"查看",
            skin:'demo-class',
            closeBtn: 1,
            shift: 2,
            area: ['885px', '500px'],
            shadeClose: true,
            btn:['确定', '取消'],
            content: $("#xjxmDiv")
        });
    }
    function parseTime(time) {
        if(!time){
           return "--";
        }
        return formatTime(time);
    }
    function loadLineList(){
        $.get(baseUrl+'/insPointRecord/getLineList', {}, function (result) {
            if(result != null){
                if(result.data != null){
                    a$opt_html += "<option  value=''>全部</option>";
                    $.each(result.data, function (index, item) {
                        a$opt_html += "<option value='" + item.lineId + "'>" + item.lineName + "</option>";
                        var obj = {"key":item.lineId, "name":item.lineName};
                        if(!hasDictKey(item.lineId)){
                            dictTypeArray.push(obj);
                        }
                        $("#inspect_result").empty().append(a$opt_html);
                        form.render('select');
                    });

                }
            }
        });
    }
});
var a$opt_html;
var dictTypeArray = [];

function hasDictKey(key) {
    for(var i in dictTypeArray){
        if(dictTypeArray[i].key === key){
            return true;
        }
    }
    return false;
}
function reload(){
    $("#line_name").val("");
    location.reload();
}
function parseResult(status){
    var state = Number(status);
    switch (state){
        case 0: return '早巡';
        case 1: return '准时';
        case 2: return '晚巡';
        default: return "--";
    }
}
function parseIfMiss(status){
    var state = Number(status);
    switch (state){
        case 0: return '否';
        case 1: return '是';
        default: return "--";
    }
}
function parseState(time){
    if(time){ return "已巡";
    }else { return "未巡"}
}